/**
 *
 */
package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.DrPatientRemarkname;
import com.naiterui.ehp.bs.doctor.dao.IDrPatientRemarkDao;
import com.naiterui.ehp.bs.doctor.repository.IDrPatientRemarkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Description 患者备注信息Dao

 * @since 1.5
 */
@Component
public class AdDrPatientRemarkDaoImpl implements IDrPatientRemarkDao {

    @Autowired
    private IDrPatientRemarkRepository drPatientRemarkRepository;

    @Override
    public void saveOrUpdate(DrPatientRemarkname remarkname) {
        if(remarkname.getId() == null){
            drPatientRemarkRepository.save(remarkname);
        } else {
            drPatientRemarkRepository.update(remarkname);
        }
    }

    @Override
    public void updateRemarknameByHql(Long doctorId, Long patientId, String remarkName) {
        DrPatientRemarkname drPatientRemarkname = drPatientRemarkRepository.findFirstByDoctorIdAndPatientId(doctorId, patientId);
        drPatientRemarkname.setRemarkName(remarkName);
        drPatientRemarkname.setChangedAt(new Date());
        drPatientRemarkRepository.update(drPatientRemarkname);
    }

    /**
     * 读取备注信息
     * @Description
     * @param doctorId
     * @param patientId
     * @return
     * @History 2017年6月13日 by wangchuan
     */
    @Override
    public DrPatientRemarkname getRemarkname(Long doctorId, Long patientId) {
        List<DrPatientRemarkname> resultList = drPatientRemarkRepository.findByDoctorIdAndPatientId(doctorId,patientId);
        if (null != resultList && !resultList.isEmpty()) {
            return resultList.get(0);
        }
        return null;
    }

    @Override
    public List<DrPatientRemarkname> getRemarkInfoList(Long doctorId, List<Long> patientIds) {
        /* 如果患者id不为空 */
        if (patientIds != null && patientIds.size() > 0) {
            return drPatientRemarkRepository.findByDoctorIdAndPatientIdIn(doctorId, patientIds);
        }
        return drPatientRemarkRepository.findByDoctorId(doctorId);
    }

    @Override
    public void remove(Long doctorId, Long patientId) {
        drPatientRemarkRepository.deleteByDoctorIdAndPatientId(doctorId, patientId);
    }

}
